package cn.ym.controller;

import cn.ym.domain.ArticleType;
import cn.ym.exception.BusinessException;
import cn.ym.query.ArticleTypeQuery;
import cn.ym.service.ArticleTypeService;
import cn.ym.utils.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 文章类型的接口
 */
@RestController
@RequestMapping("/articleType")
@Api(value = "文章类型的接口的API",description="文章类型的接口的CRUD功能")
public class ArticleTypeController {

    @Autowired
    private ArticleTypeService articleTypeService;

    //添加或修改文章类型信息
    @PutMapping
    @ApiOperation(value = "添加+修改" )
    public AjaxResult save(@RequestBody ArticleType articleType){
        try {
            if(articleType.getId()==null){
                articleTypeService.add(articleType);
            }else {
                articleTypeService.update(articleType);
            }
            return new AjaxResult();
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试");
        }

    }

    //删除文章类型信息
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            articleTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

    //查询所有文章类型信息
    @ApiOperation(value = "查询使用文章类型" )
    @GetMapping
    public List<ArticleType> findAll(){
        return articleTypeService.findAll();
    }

    //查询单个文章类型信息
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public ArticleType findOne(@PathVariable("id") Long id){
        return articleTypeService.findOne(id);
    }


    //分页查询 + 高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级" )
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery){
        List<ArticleType> articleTypes = articleTypeService.queryPage(articleTypeQuery);
        return new PageInfo<>(articleTypes);
    }

    //批量删除文章类型信息
    @ApiOperation(value = "通过ID删除" )
    @PatchMapping
    public AjaxResult Patchdel(@RequestBody Long[]  ids){
        try {
            articleTypeService.Patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }
}